/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.ac.cput.chapter5.designpatters.structuralpattern.flyweightpattern;

import java.util.HashMap;
import java.util.Map;
import za.ac.cput.chapter5.designpatters.structuralpattern.flyweightpattern.flyweightimpl.FlyweightAdder;
import za.ac.cput.chapter5.designpatters.structuralpattern.flyweightpattern.flyweightimpl.FlyweightMultiplier;
import za.ac.cput.chapter5.designpatters.structuralpattern.flyweightpattern.flyweigthinterface.Flyweight;

/**
 *
 * @author Sakhe
 */
public class FlyweightFactory {
    
    private static FlyweightFactory flyweightFactory ;
    private Map<String,Flyweight> flyweightPool;
    
    private FlyweightFactory(){
    flyweightPool = new HashMap<String,Flyweight>();
    }
    public static FlyweightFactory getInstance(){
    if(flyweightFactory == null){
    flyweightFactory = new FlyweightFactory();
    }
    return flyweightFactory;
    }
    public Flyweight getFlyweight(String key){
    if(flyweightPool.containsKey(key)){
    return flyweightPool.get(key);
    }else{
    Flyweight flyweight;
    if("dd".equals(key)){
    flyweight = new FlyweightAdder();
    }else{
    flyweight = new FlyweightMultiplier();
    }
    flyweightPool.put(key,flyweight);
    return flyweight;
    }
    }
}
